/*
* 
 * Author: Mishal Parmar
 * Date: 27 Oct 2012
 */
package controllers;

import dbhandler.ElectionManage;
import dbhandler.EventManage;

import entityclasses.election;
import entityclasses.event;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author : Mishal Parmar
 * Date:  22 Oct 2012
 */
@WebServlet(name = "createElection", urlPatterns = {"/createElection"})
public class createElection extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException, SQLException, ClassNotFoundException{
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet create</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet create at " + request.getContextPath() + "</h1>");
            
        election newelection=new election();
        
        EventManage evt=new EventManage();
        ArrayList evtarr=new ArrayList();
        
        
        
        java.util.Date dt = new java.util.Date();
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String currentTime = sdf.format(dt);
        
        DateFormat formatter ; 
        
        Date date ; 
        formatter = new SimpleDateFormat("MM-dd-yyyy HH:mm");
       
        event newevent=new event();
        
        int eventid=0;
        
        newevent.setAccessId(Integer.parseInt(request.getParameter("accesstype")));
        newevent.setTypeId(Integer.parseInt(request.getParameter("eventtype")));
        newevent.setPosteddt(currentTime);
                              	        
        
        String[] datetime={request.getParameter("starttime"),         
        request.getParameter("endtime"),
        request.getParameter("nomineestart"),
        request.getParameter("nomineeend")};
        
        EventManage add= new EventManage();
        
         if(add.addeventquery(newevent))
         out.println("done event");
         
        
        
        evtarr=evt.eventidbydate(currentTime);
        
        
        event event=new event();
        
        
        out.println(evtarr.size());
        
        
        for(int i=0;i<evtarr.size();i++)
         {
            
        event= (event) evtarr.get(i);
        
        eventid=event.getEventId();
        out.println(eventid);
         }
        
        for(int i=0;i<4;i++)
        {
            
        date =(Date)formatter.parse(datetime[i]);      
        java.text.SimpleDateFormat datefor = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timedate = datefor.format(date);
        datetime[i]=timedate;
        }
 
        
        newelection.setStart_time(datetime[0]);
        newelection.setEnd_time(datetime[1]);
        newelection.setNominee_start(datetime[2]);
        newelection.setNominee_end(datetime[3]);
        newelection.setCommissioner_id(201112078);
        newelection.setElect_id(eventid);
        
            
        out.println(newelection.getStart_time());
        
        
        ElectionManage addelect= new ElectionManage();
        
         if(addelect.addElectionquery(newelection))
        out.print("Done Election");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
                    try {
                        processRequest(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
                    }
        } catch (ParseException ex) {
            Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
                    try {
                        processRequest(request, response);
                    } catch (SQLException ex) {
                        Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
                    } catch (ClassNotFoundException ex) {
                        Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
                    }
        } catch (ParseException ex) {
            Logger.getLogger(createElection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
